package entity.weapons;

import java.awt.Color;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Date;

import entity.*;

import levels.Level;
import main.GameList;
import math.Ray;
import math.Vector2D;

public class Gun
{
	private int damage;
	//time in milliseconds
	private double reloadTime;
	private long time;
	private GameList list;
	private Level level;
	//store the shots so i can draw them
	private ArrayList<ArrayList<Vector2D>> shots;
	private ArrayList<Integer> alphas;
	
	private double timeUntilShoot;
	
	private int maxLength;
	
	public Gun(int damage, double reloadTime, Level lvl, GameList gl)
	{
		this.damage = damage;
		this.reloadTime = reloadTime;
		list = gl;
		level = lvl;
		maxLength = (int)(lvl.getMapBounds().getWidth() + lvl.getMapBounds().getHeight());
		shots = new ArrayList<ArrayList<Vector2D>>();
		alphas = new ArrayList<Integer>();
	}
	
	/**
	 * Shoot at a certain angle, hit anyone in the way
	 * @param position
	 * @param angle
	 */
	public void shoot(Entity exclude, Vector2D position, double angle)
	{
		//don't shoot if trying too quick
		if(timeUntilShoot <= 0)
		{
			time = new Date().getTime();
			timeUntilShoot = reloadTime;
		}
		else
		{
			timeUntilShoot -= new Date().getTime() - time;
			time = new Date().getTime();
			if(timeUntilShoot >= 0)
				return;
		}
		
		//add the position
		shots.add(new ArrayList<Vector2D>());
		shots.get(shots.size()-1).add(position);
		
		ArrayList<Entity> people = new ArrayList<Entity>();
		Ray ray = new Ray(position, angle);
		Vector2D vect = new Vector2D(1,1);
		vect.setAngle(angle);
		vect.setMagnitude(maxLength);
		
		for(Entity e : list.getList())
		{
			//see if the line intersects an entity
			if(e != exclude && e.getBounds().intersectsLine(position.getX(), position.getY(), position.getX() + vect.getX(), position.getY() + vect.getY()))
			{
				//add it to our list
				people.add(e);
			}
		}
		
		//find the closest one
		if(people.size() != 0)
		{
			double closestDist = maxLength;
			int index = 0;
			
			for(int i=0;i<people.size();i++)
			{
				if(position.distance(people.get(i).getPosition()) < closestDist)
				{
					closestDist = position.distance(people.get(i).getPosition());
					index = i;
				}
			}
			((DynamicEntity) people.get(index)).damage(damage);
			
			//add the end point
			//we want the bullet to end around the person, so it's not always perfectly on target when drawn
			Vector2D toPerson = Vector2D.subtract(people.get(index).getPosition(), position);
			toPerson.setAngle(angle);
			shots.get(shots.size()-1).add(Vector2D.add(position, toPerson));
		}
		else
		{
			//add the end point
			shots.get(shots.size()-1).add(level.findRayEnd(new Ray(position, angle)));	
		}
		alphas.add(255);
	}
	
	public void draw(Graphics2D g)
	{
		Color c = g.getColor();
		//draw the line representing the shot
		for(int i=0;i<shots.size();i++)
		{
			g.setColor(new Color(Color.white.getRed(), Color.white.getGreen(), Color.white.getBlue(), (int)alphas.get(i)));
			alphas.set(i, alphas.get(i) - 30);
			g.drawLine((int)shots.get(i).get(0).getX(), (int)shots.get(i).get(0).getY(), 
					(int)shots.get(i).get(1).getX(), (int)shots.get(i).get(1).getY());
			
			if(alphas.get(i) <= 0)
			{
				alphas.remove(i);
				shots.remove(i);
				i--;
			}
		}
		g.setColor(c);
	}
}
